import Promise from 'bluebird';

import service from '../service/group';
import CONSTANT from '../constant/group';

const addGroup = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.addGroup(data).then((res) => {
      commit(CONSTANT.GROUP_ADD_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_ADD_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const addGroupCollection = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.addGroupCollection(data).then((res) => {
      commit(CONSTANT.GROUP_COLLECTION_ADD_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_COLLECTION_ADD_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const uploadImg = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.uploadImg(data).then((res) => {
      commit(CONSTANT.GROUP_UPLOAD_IMG_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_UPLOAD_IMG_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getGroup = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getGroup(data).then((res) => {
      commit(CONSTANT.GROUP_GET_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_GET_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getGroupCollection = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getGroupCollection(data).then((res) => {
      commit(CONSTANT.GROUP_COLLECTION_GET_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_COLLECTION_GET_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getPublicAccountsWithDefault = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getPublicAccountsWithDefault(data).then((res) => {
      commit(CONSTANT.PUBLICACCOUNT_GET_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.PUBLICACCOUNT_GET_ERROE, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getGroupDetail = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getGroupDetail(data).then((res) => {
      commit(CONSTANT.GROUP_DETAIL_GET_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_DETAIL_GET_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const editGroupDetail = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.editGroupDetail(data).then((res) => {
      commit(CONSTANT.GROUP_DETAIL_EDIT_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_DETAIL_EDIT_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const editGroupCollectionDetail = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.editGroupCollectionDetail(data).then((res) => {
      commit(CONSTANT.GROUP_COLLECTION_DETAIL_EDIT_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_COLLECTION_DETAIL_EDIT_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getGroupCollectionDetail = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getGroupCollectionDetail(data).then((res) => {
      commit(CONSTANT.GROUP_COLLECTION_DETAIL_GET_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_COLLECTION_DETAIL_GET_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getGroupImgById = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getGroupImgById(data).then((res) => {
      commit(CONSTANT.GROUP_GET_IMG_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.GROUP_GET_IMG_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getGroupCollectionByAppId = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getGroupCollectionByAppId(info).then((res) => {
      commit(CONSTANT.GROUP_GET_COLLECTION_BY_APPID_SUCCESS, res);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};

const changeGroupActive = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.changeGroupActive(info).then((res) => {
      commit(CONSTANT.GROUP_CHANGE_ACTIVE, res);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};

const changeGroupCollectionActive = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.changeGroupCollectionActive(info).then((res) => {
      commit(CONSTANT.GROUP_COLLECTION_CHANGE_ACTIVE, res);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};

export default {
  addGroup,
  addGroupCollection,
  uploadImg,
  getGroup,
  getGroupCollection,
  getPublicAccountsWithDefault,
  getGroupDetail,
  getGroupCollectionDetail,
  editGroupDetail,
  editGroupCollectionDetail,
  getGroupImgById,
  getGroupCollectionByAppId,
  changeGroupActive,
  changeGroupCollectionActive,
};
